/**
 * Created by lixiaodi on 2018/9/27 0027.
 */
    mini.parse();                                       //将html标签解析为miniui的控件
    var groupGrid = mini.get("groupContent");           //得到页面上id为groupContent的元素

    //groupGrid.set({'url':'http://192.168.178.103:8080/bpm/GroupsController/getList.do'}); 数据表中的路径的另一种设置方法
    groupGrid.load(ajaxUrl + "/GroupsController/getList.do");   //加载数据(显示数据){sender: Object, //表格对象xhr: Object,    //ajax对象 data: Array     //数据}
    groupGrid.load();
    console.log(groupGrid.getData());
    var url = bootPATH + "../frame/frame1/pages/";      //弹出窗口路径前缀
    var menu = new ColumnsMenu(groupGrid);
//增加
    function add() {
        mini.open({
            url: url + "alertBox/groupWindow.html",
            title: "新增集团", width: 600, height: 400,
            onload: function () {
                //getResultObject();获取加载的服务端数据对象
                var iframe = this.getIFrameEl();
                var data = { action: "new"};
                iframe.contentWindow.SetData(data);
            },
            ondestroy: function (action) {
                //子页面关闭的时候，父页重新加载
                groupGrid.reload();
            }
        });
    }
//编辑
    function edit() {
        var row = groupGrid.getSelected();
        console.log(row);
        if (row) {
            mini.open({
                url: url + "alertBox/groupWindow.html",
                title: "编辑集团", width: 600, height: 400,
                onload: function () {
                    var iframe = this.getIFrameEl();
                    var data = { action: "edit", id: row.groupId};
                    //把值赋给子页面
                    iframe.contentWindow.SetData(data);
                },
                ondestroy: function (action) {
                    groupGrid.reload();
                }
            });

        } else {
            mini.alert("请选中一条记录");
        }

    }
//删除
    function remove() {
        var rows = groupGrid.getSelecteds();
        if (rows.length > 0) {
            mini.confirm("确定删除选中记录？","系统提示",function(action){
                if (action == "ok") {
                    var ids = [];
                    for (var i = 0, l = rows.length; i < l; i++) {
                        var r = rows[i];
                        ids.push(r.groupId);
                    }
                    var id = ids.join(',');
                    groupGrid.loading("操作中，请稍后......");
                    $.ajax({
                        url: ajaxUrl + "/GroupsController/deletes.do?ids=" + id,
                        type:"post",
                        success: function (text) {
                            groupGrid.reload();
                        },
                        error: function () {
                            mini.alert('失败')
                        }
                    });
                }
            })
        } else {
            mini.alert("请选中一条记录");
        }
    }
//查询
    function search() {
        var key = mini.get("key").getValue();
        //groupGrid.load({ key: key });
        $.ajax({
            url: ajaxUrl + "/GroupsController/getListByParam.do",
            type:"post",
            datatype:"json",
            data:{
                groupName:key
            },
            success: function (data) {
                groupGrid.setData(data.data)        //设置数据
                //groupGrid.reload();       //页面重载
            },
            error: function () {
                mini.alert("失败");
            }
        });
    }
//enter的时候查询
    function onKeyEnter(e) {
        search();
    }


//得到树
var tree = mini.get("leftTree");
function activeTab(item) {
    var tabs = mini.get("mainTabs");

    var tab = tree.getTab(item.id);
    if (!tab) {
        tab = { name: item.id, title: item.text, url: item.url, iconCls: item.iconCls, showCloseButton: true };
        tab = tabs.addTab(tab);
    }
    tabs.activeTab(tab);
}

function showTab(node) {
    var tabs = mini.get("mainTabs");
    console.log(tabs)
    var id = "tab$" + node.id;
    var tab = tabs.getTab(id);
    if (!tab) {
        tab = {};
        tab._nodeid = node.id;
        tab.name = id;
        tab.title = node.text;
        tab.showCloseButton = true;
        //这里拼接了url，实际项目，应该从后台直接获得完整的url地址
        tab.url = mini_JSPath + "../../docs/api/" + node.id + ".html";
        //tab.url="../pages/groupRun.html";

        tabs.addTab(tab);
    }
    tabs.activeTab(tab);
}

function onNodeSelect(e) {

    var node = e.node;
    var isLeaf = e.isLeaf;

    if (isLeaf) {
        showTab(node);
    }
}


function onClick(e) {
    var text = this.getText();
    alert(text);
}
